package com.hdoit.ares.web.team.controllers;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hdoit.ares.logical.cust.model.Customer;
import com.hdoit.ares.logical.cust.service.ICustomerService;
import com.hdoit.ares.logical.team.model.CooperationTeam;
import com.hdoit.ares.logical.team.service.ICooperationTeamService;
import com.hdoit.ares.logical.viewmodel.team.CooperationTeamSearch;
import com.hdoit.ares.selfcommon.enums.cust.CustomerLevel;
import com.tennetcn.common.enums.ManageContainer;
import com.tennetcn.common.enums.ModelStatus;
import com.tennetcn.common.enums.pub.SexStatus;
import com.tennetcn.common.logical.setting.model.ParamOption;
import com.tennetcn.common.logical.setting.service.IParamOptionService;
import com.tennetcn.common.logical.superbase.service.IRoleEmployeeService;
import com.tennetcn.common.logical.viewmodel.setting.ParamOptionSearch;
import com.tennetcn.common.message.PagerModel;
import com.tennetcn.common.utils.ChineseUtils;
import com.tennetcn.common.utils.CommonUtils;
import com.tennetcn.common.utils.DateUtils;
import com.tennetcn.common.utils.JsonUtils;
import com.tennetcn.component.web.ActionResult;
import com.tennetcn.component.web.BaseController;
import com.tennetcn.component.web.ErrorResult;
import com.tennetcn.component.web.JsonActionResult;

/**
 * @author      yujinhui
 * @email       
 * @createtime  2018年01月18日 14:34:27
 * @comment
 */
@Controller
@RequestMapping(value = "/admin/team/cooperationTeam/")
public class CooperationTeamController extends BaseController{
    @Override
    public String getRoutePath() {
        return "areas/admin/team/cooperationTeam/";
    }

    private ICooperationTeamService cooperationTeamService;
    public ICooperationTeamService getCooperationTeamService() {
        return cooperationTeamService;
    }
    @Resource
    public void setCooperationTeamService(ICooperationTeamService cooperationTeamService) {
        this.cooperationTeamService = cooperationTeamService;
    }
    
    private IRoleEmployeeService roleEmployeeService;	
	public IRoleEmployeeService getRoleEmployeeService() {
		return roleEmployeeService;
	}	
	@Resource
	public void setRoleEmployeeService(IRoleEmployeeService roleEmployeeService) {
		this.roleEmployeeService = roleEmployeeService;
	}
	
	private IParamOptionService paramOptionService;
	public IParamOptionService getParamOptionService() {
		return paramOptionService;
	}
	@Resource
	public void setParamOptionService(IParamOptionService paramOptionService) {
		this.paramOptionService = paramOptionService;
	}
	
	private ICustomerService customerService;
    public ICustomerService getCustomerService() {
		return customerService;
	}
    @Resource
	public void setCustomerService(ICustomerService customerService) {
		this.customerService = customerService;
	}

	private final String indexViewName="index";
    private final String outputViewName="output";
    private final String detailedViewName="cooperationTeamDetailed";
    private final String saveViewName="save";
    private final String deleteViewName = "delete";
    private final String provinceCityViewName = "provinceCity";

    @RequestMapping(value = indexViewName, method = { RequestMethod.GET,RequestMethod.POST })
    public String index(Model model,PagerModel pagerModel,CooperationTeamSearch search,boolean ajaxPost){


		String employeeAccount = getLoginModel().getLoginAccount();
		System.out.println(employeeAccount);
		pagerModel.setTotalAmount(cooperationTeamService.queryCount(search));
		List<CooperationTeam> cooperationTeams = cooperationTeamService.queryList(search, pagerModel, employeeAccount);
		model.addAttribute(ManageContainer.PAGERKEY, pagerModel);
		model.addAttribute("cooperationTeams", cooperationTeams);
		model.addAttribute("cooperationTeamsSearch", search);
		
		if (ajaxPost) {
			return view(outputViewName);
		}
		return view(indexViewName);
    }

    @RequestMapping(value = detailedViewName, method = { RequestMethod.GET})
    public String cooperationTeamDetailed(Model model,String key){
    	CooperationTeam cooperationTeam=new CooperationTeam();
    	cooperationTeam.setCreateDate(DateUtils.getCurrent());
    	cooperationTeam.setSex(SexStatus.male);
        System.out.println("employeeId" + key);
        
        if(!StringUtils.isEmpty(key)){
        	cooperationTeam=cooperationTeamService.queryModel(key);
            List<String> roleIds = roleEmployeeService.queryRoleIdsByEmployeeId(key);
            model.addAttribute("roleIds",roleIds);
        }
        
        model.addAttribute("cooperationTeam", cooperationTeam);
       
        return view(detailedViewName);
    }

    @RequestMapping(value = saveViewName, method = { RequestMethod.POST})
    public String save(Model model,CooperationTeam cooperationTeam){
    	if(StringUtils.isEmpty(cooperationTeam.getEmployeeId())){
    		Customer customer = customerService.queryModelByUserIdentity(cooperationTeam.getUserIdentity());
    		//高创
    		if(customer != null && CustomerLevel.highMaker.equals(customer.getLevel())){
    			System.err.println("查看主键是否一致:\t" +customer.getId() + "查看用户编号:\t" + customer.getUserIdentity());
    			cooperationTeam.setEmployeeId(customer.getId());
    		}else{
    			//城市合伙人
    			cooperationTeam.setEmployeeId(CommonUtils.getKeyId());	
    		}
    		
    		
    		
    		cooperationTeam.setCreateDate(DateUtils.getCurrent());
    		cooperationTeam.setCreateUserId(getLoginModel().getLoginId());
    		cooperationTeam.setCreateUserName(getLoginModel().getLoginName());
    		cooperationTeam.setModelStatus(ModelStatus.add);       	
        }else{
        	cooperationTeam.setModifyDate(DateUtils.getCurrent());
        	cooperationTeam.setModifyUserId(getLoginModel().getLoginId());
        	cooperationTeam.setModifyUserName(getLoginModel().getLoginName());
        	cooperationTeam.setModelStatus(ModelStatus.update);
        }
        //markcode
    	cooperationTeam.setMarkCode(ChineseUtils.getPinYinHeadChar(cooperationTeam.getEmployeeName()));
        
        ActionResult actionResult;
        if(cooperationTeamService.applyChange(cooperationTeam)){
            actionResult=new ActionResult();
        }else{
            actionResult=new ErrorResult();
        }
        model.addAttribute("personnel", cooperationTeam);
        model.addAttribute(ManageContainer.RESULTKEY, actionResult);
        return view(detailedViewName);
    }

    @RequestMapping(value =deleteViewName, method = { RequestMethod.POST },produces="text/plain;charset=utf-8")
    public @ResponseBody String delete(String key){
        if(cooperationTeamService.deleteFakeByKey(key)){
            return new JsonActionResult(ManageContainer.SUCCESS).toJson();
        }
        return new JsonActionResult(ManageContainer.ERROR).toJson();
    }
    
    @RequestMapping(value = provinceCityViewName, method = { RequestMethod.GET,RequestMethod.POST},produces="text/plain;charset=utf-8")
	@ResponseBody
	public String provinceCity(String keyId){
	    ParamOptionSearch paramOptionSearch=new ParamOptionSearch();
		
	    if("all".equals(keyId)){
			paramOptionSearch.setParamName("province");
		}else{
			paramOptionSearch.setParentId(keyId);
			System.err.println("keyId是什么"+keyId);
		}
    	
    	
    	List<ParamOption> paramOptions=paramOptionService.queryList(paramOptionSearch);
    	
    	return JsonUtils.toJson(paramOptions);
 }
}